﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling.Primitives;
using System.Drawing;
using ITAP.ShprossesBuilder.Editor;
using System.Drawing.Drawing2D;
using ITAP.ShprossesBuilder.Modelling.Measurement;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Render
{
    public class DraftRender : Render
    {
        public override void Initialize(Graphics graphics, int screenWidth, int screenHeight, RectangleF modelRectangle, ModelCoordinateConverter coordinateConverter, ModelPoint centerPoint)
        {
            this.totalBoundingBoxHelper.Clear();
            this.boundingBoxAfterDrawing = RectangleF.Empty;
            this.centerPoint = centerPoint;
            this.graphics = graphics;
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
            this.modelRectangle = modelRectangle;
            this.coordinateConverter = coordinateConverter;
            this.coordinateConverter.RecalculateAutoScale(screenWidth, screenHeight, modelRectangle);
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;            
        }
        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
            DimensionProcessor.Compute(modelCell, 1, DimensionSpreadingType.ByNearestSide, dimensionsPreferences);
            this.SetBrush(Color.FromArgb(240, 240, 240));
            this.Visit(modelCell.Model);
            this.DrawDimensions(modelCell);

        }
        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            LinePrimitive connectorPrimitive = connector.Primitives[0] as LinePrimitive;
            this.DrawLinePrimitive(connectorPrimitive, this.GetDecolorizedColor(connector.Color),
                                   this.GetDecolorizedColor(connector.BorderColor), 0, 1, true, true);
        }

        protected override void DrawShpross(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross, bool drawStartBorder, bool drawEndBorder)
        {
            if (shpross.Height != 0)
            {
                CirclePrimitive shprossCirclePrimitive = shpross.Primitives[0] as CirclePrimitive;
                this.DrawCirclePrimitive(shprossCirclePrimitive, shpross.Color, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
            }
            else
            {
                LinePrimitive shprossLinePrimitive = shpross.Primitives[0] as LinePrimitive;
                this.DrawLinePrimitive(shprossLinePrimitive, shpross.Color, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
            }
        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            this.DrawShpross(shpross, true, true);
        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
            GraphicsPath path = new GraphicsPath();
            PointF point1;
            PointF point2;
            path.StartFigure();
            foreach (IPrimitive primitive in stub.Primitives[0].InnerPrimitives)
            {
                point1 = coordinateConverter.ModelPointToScreenPoint(primitive.GetPoint(0), screenWidth, screenHeight, modelRectangle, this.centerPoint);
                point2 = coordinateConverter.ModelPointToScreenPoint(primitive.GetPoint(1), screenWidth, screenHeight, modelRectangle, this.centerPoint);
                path.AddLine(point1, point2);
            }
            path.CloseFigure();
            this.SetBrush(this.GetDecolorizedColor(stub.Color));
            graphics.FillPath(brush, path);
            this.SetPen(this.GetDecolorizedColor(stub.BorderColor), 1);
            graphics.DrawPath(pen, path);

        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.SnapOnCap snapOnCap)
        {
            List<PointF> points = new List<PointF>();
            List<byte> bytes = new List<byte>();
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            foreach (IEndedPrimitive line in snapOnCap.Primitives[0].InnerPrimitives)
            {
                PointF point1 = coordinateConverter.ModelPointToScreenPoint(line.StartPoint, screenWidth, screenHeight, modelRectangle, this.centerPoint);
                PointF point2 = coordinateConverter.ModelPointToScreenPoint(line.EndPoint, screenWidth, screenHeight, modelRectangle, this.centerPoint);
                path.AddLine(point1, point2);
            }

            this.SetBrush(this.GetDecolorizedColor(snapOnCap.Color));
            graphics.FillPath(brush, path);
            this.SetPen(this.GetDecolorizedColor(snapOnCap.BorderColor), 1);
            graphics.DrawPath(pen, path);
        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {            
        }               

        public Color GetDecolorizedColor(Color color)
        {
            int average = (color.A + color.G + color.B) / 3;
            return Color.FromArgb(255, average, average, average);
        }

        public override void Visit(ITAP.ShprossesBuilder.Modelling.Components.CrossConnector crossConnector)
        {
            
        }
    }

}
